import {WechatyBuilder} from 'wechaty'
import QrcodeTerminal from "qrcode-terminal";
import {ask} from './sparkutil.js'


//


// 实例化机器人
const bot = WechatyBuilder.build({name: 'wh-bot'}) //

// 添加监听事件
bot
    // 扫码登录
    .on("scan", (qrcode, status) => {
        console.log(
            `扫码二维码进行登录\n ================================================: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(
                qrcode
            )}`
        );
        QrcodeTerminal.generate(qrcode);
        console.log(
            `扫码二维码进行登录\n ================================================: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(
                qrcode
            )}`
        );
    })
    // 登录监听
    .on("login", (user) => {
        console.log(user, "logined");
    })
    // 退出监听
    .on("logout", (user) => {
        console.log(user, "logout");
    })
    // 消息监听
    .on("message", message => {
        const room = message.room();
        let text = message.text();
        let talker
        if (room) {
            if (text.includes("@繁华")) {
                text = text.replace("@繁华", "")
                talker = room
            }
        } else if (message.type() === 7) {
            talker = message.talker()
        }
        // 询问
        if (talker) {
            ask(text).then(function (answer) {
                talker.say(answer)
            })
        }
    })
    .start();

